package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.wms.dto.WarehouseDto;
import com.ruoyi.wms.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wms.domain.Warehouse;
import com.ruoyi.wms.service.IWarehouseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 仓库Controller
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@Api(value = "仓库Controller", tags = "仓库Controller")
@RestController
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 查询仓库列表
     */
    @RequiresPermissions("wms:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseDto warehouse) {
        startPage();
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return getDataTable(list);
    }


    /**
     * 获取仓库详细信息
     */
    @RequiresPermissions("wms:warehouse:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(warehouseService.selectWarehouseById(id));
    }


    /**
     * 修改仓库
     */
    @RequiresPermissions("wms:warehouse:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehouse warehouse) {
        return toAjax(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除仓库
     */

    @ApiOperation("删除仓库")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return error("未选中");
        }
        return toAjax(warehouseService.deleteWarehouseByIds(ids));
    }

//时间


    @ApiOperation("生成仓库-库区树形结构")
    @GetMapping("/tree")
    public AjaxResult   buildWarehouseTree() {
        try {
            List<TreeVo> tree = warehouseService.buildWarehouseTree();
            return AjaxResult.success("获取树形结构成功", tree);
        } catch (Exception e) {
            System.out.println("构建仓库树形结构失败");
            return AjaxResult.error("构建树形结构失败");
        }
    }

    @ApiOperation("获取仓库列表（平铺数据）")
    @GetMapping("/queryWarehouseList")
    public AjaxResult queryWarehouseList() {
        try {
            List<Map<String, Object>> list = warehouseService.getWarehouseList();
            return AjaxResult.success("获取仓库列表成功", list);
        } catch (Exception e) {
//            log.error("获取仓库列表失败", e);
            return AjaxResult.error("获取仓库列表失败");
        }
    }

}
